package com.yuerlive.rainbow.utils;

import android.app.Activity;
import android.widget.Toast;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.ShareContent;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.yuerlive.rainbow.RainbowApp;
import com.yuerlive.rainbow.entity.ShareInfo;
import com.yuerlive.rainbow.observer.Observerable;
import com.yuerlive.rainbow.observer.ObserverableType;

/**
 * Created by liux on 2017/5/18.
 */
public class ShareUtils {

    /**
     * 存放分享的渠道
     */
    final static SHARE_MEDIA[] displaylist = new SHARE_MEDIA[]{SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SINA, SHARE_MEDIA.QQ,
            SHARE_MEDIA.QZONE};

    public static void sharaToSocialAll(Activity activity, UMImage imageUrl) {
        new ShareAction(activity).setDisplayList(displaylist).withMedia(imageUrl).setCallback(umShareListener).open();
    }

    public static void shareToSocial(Activity activity, SHARE_MEDIA platform, ShareInfo info) {

        UMWeb web = new UMWeb(info.getWebUrl());
        web.setTitle(info.getTitle());
        web.setThumb(new UMImage(activity, info.getShareImageUrl()));
        new ShareAction(activity).withText(info.getTitle()).withMedia(web).setPlatform(platform)
                .setCallback(umShareListener)
                .share();
    }
    public static void shareToUrl(Activity activity, SHARE_MEDIA platform, ShareInfo info){
        String url=info.getWebUrl();
        if (url.contains("?")) {
            url = url + "&";
        } else {
            url = url + "?";
        }
        url = url + "userId=" + UserProxy.getUser().getUserId();
        L.e("ShareUtils","URL"+url);
        UMWeb web = new UMWeb(url);
        web.setTitle(info.getTitle());
        web.setDescription(info.getContent());
//         UMImage image = new UMImage(activity, info.getShareImageUrl());//网络图片
//        image.setThumb(new UMImage(activity, info.getThumb()));
//        web.setThumb(image);
        web.setThumb(new UMImage(activity, info.getShareImageUrl()));
        new ShareAction(activity).withMedia(web).setPlatform(platform)
                .setCallback(umShareListener)
                .share();
    }
    public static void shareToSocialImage(Activity activity, SHARE_MEDIA platform, ShareInfo info) {

        UMImage image = new UMImage(activity, info.getBitmap());
        image.setTitle(info.getTitle());
        image.setThumb(new UMImage(activity, info.getThumb()));
        new ShareAction(activity).withText(info.getTitle()).withMedia(image).setPlatform(platform)
                .setCallback(umShareListener)
                .share();
    }

    private static UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            L.e("ShareUtils","onStart");
            //	Toast.makeText(RainbowApp.getContext(), platform + " 分享开始啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            L.e("ShareUtils","onResult");
            Toast.makeText(RainbowApp.getContext(), /*platform + */" 分享成功", Toast.LENGTH_SHORT).show();
            Observerable.getInstance().notifyChange(ObserverableType.SHARE,1);
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            L.e("ShareUtils","onError");
            Toast.makeText(RainbowApp.getContext(), /*platform + */" 分享失败", Toast.LENGTH_SHORT).show();
            Observerable.getInstance().notifyChange(ObserverableType.SHARE,null);
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            L.e("ShareUtils","onCancel");
            Toast.makeText(RainbowApp.getContext(), /*platform + */" 分享取消", Toast.LENGTH_SHORT).show();
            Observerable.getInstance().notifyChange(ObserverableType.SHARE,null);
        }
    };
}
